from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models

# Create your models here.



class Teacher(models.Model):
    # 个人基本信息的模型
    # id
    sid = models.CharField(db_column='Sid', primary_key=True, null=False, max_length=200, unique=True)
    # 用户名
    name = models.CharField(db_column='Sname', null=True, max_length=200)
    # 真实姓名
    rname = models.CharField(db_column='really_name', null=False, max_length=200)
    # 定义限制内容(性别)
    SEX_CHOOSE = [
        ('男', '男'),
        ('女', '女')
    ]
    sex = models.CharField(choices=SEX_CHOOSE, verbose_name='性别', null=True, max_length=4)
    # 级别
    level = models.CharField(db_column='Level', null=True, max_length=200)
    birthday = models.DateField(db_column="Birthday", null=True)
    # 依次往下是电话， 籍贯， 邮箱
    phone = models.CharField(db_column='Phone', null=False, max_length=13)
    origin = models.CharField(db_column='Origin', null=False, max_length=200)
    mailbox= models.CharField(db_column='Mailbox', null=True, max_length=200)


    # 下面是教师的教育信息的模型
    # 毕业院校
    by_school = models.CharField(db_column='By_school', null=True, max_length=200)
    # 专业(还在学校就读期间选择的专业)
    major = models.CharField(db_column='Major', null=True, max_length=200)
    # 毕业院校邮政编码
    school_encode = models.CharField(db_column='School_id', null=True, max_length=200)
    # 入职时间
    hire = models.DateField(db_column='Hire', null=True)
    # 学历(入职本校时的学历)
    DEGREE_CHOOSE=[
        ('本科', '本科'),
        ('硕士', '硕士'),
        ('博士', '博士')
    ]
    degree = models.CharField(default='硕士', verbose_name='学历', choices=DEGREE_CHOOSE, null=True,  max_length=4)


#   下面是在学校的工作信息，包含职位名称， 职位ID，所属部门, 教师介绍
    posts_name = models.CharField(max_length=200, db_column='Posts_name', null=True)
    posts_id = models.CharField(max_length=200, db_column='Posts_id', null=True)
    org = models.CharField(max_length=200, db_column='Org', null=True)
    # 自我介绍
    intro = models.CharField(max_length=200, db_column='Intro', null=True)


    # 家庭信息
    f_name = models.CharField(db_column='F_name', null=True, max_length=200)
    f_phone = models.CharField(db_column='F_phone', null=True, max_length=200)
    m_name = models.CharField(db_column='M_name', null=True, max_length=200)
    m_phone = models.CharField(db_column='M_phone', null=True, max_length=200)
    address = models.CharField(db_column='Address', null=True, max_length=200)

    STATE = [
        ('离职', '离职'),
        ('在职', '在职')
    ]
    state = models.CharField(default='在职', verbose_name='教师状态', choices=STATE, null=True,  max_length=4)

    class Meta:
        #managed 属性默认为 True，
        # 表示Django会负责创建、维护和更新数据库中的这张表。
        # 如果想手动控制这张表（例如，不让Django自动创建或迁移），可以将 managed 设置为 False。
        managed = True
            #指定数据库表名 (db_table)：默认情况下，Django会根据应用名和模型名自动生成数据库表的名称，
            # 但是可以通过设置 db_table 属性来指定模型对应的实际数据库表名。
            # 例如，db_table = "Student" 表示该模型将映射到数据库中名为 Student 的表。
        db_table = "teacherapp_teacher"
        app_label = 'teacherapp'





class Teacher_user(models.Model):
    teacher = models.OneToOneField('teacherapp.Teacher', on_delete=models.CASCADE)
    password = models.CharField(db_column='Password', null=False, max_length=200)


class T_award(models.Model):
    award_id = models.CharField(db_column='Award_id', null=False, unique=True, max_length=200, primary_key=True)
    award_name = models.CharField(db_column='Award_name', null=False, max_length=300)
    award_unit = models.CharField(db_column='Award_unit', verbose_name='颁布奖项的单位', null=False, max_length=300)
    award_time = models.DateTimeField(verbose_name='颁奖时间，又教师设置',
                                      db_column='Award_time',
                                      null=False,
                                      max_length=300,
                                      auto_created=False)
    award_text = models.CharField(verbose_name='奖项的详细描述', null=False, max_length=1000)
    AWARD_LEVEL = [
        (1, '学生'),
        (2, '教师'),
    ]
    award_level = models.IntegerField(verbose_name='奖项所属对象，只有学生和老师，依次来实现单独获取学生或老师的奖项信息',
                                      null=True,
                                      choices=AWARD_LEVEL
                                      )

    # 新增外键字段，允许为空并设置默认值为 1
    root = models.ForeignKey(
        'rootone.Rootone_if',
        on_delete=models.PROTECT,
        null=True,  # 允许 NULL 值
        verbose_name='学院管理员的id'
    )

    created_at = models.DateTimeField(auto_now_add=True, null=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True,null=True, verbose_name='最后修改时间')

    exe_person = models.CharField(verbose_name='执行人', null=True, max_length=20)

    class Meta:
        db_table = 'teacherapp_t_award'



class teacher_award_zj(models.Model):
    # ForeignKey 字段不需要也不支持 max_length 参数，这是 CharField 的属性。
    teacher_name = models.ForeignKey(
        'teacherapp.Teacher',
        on_delete=models.SET_NULL,
        verbose_name='外键连接教师信息表，当教师信息被删除时，此数据也被删除',
        null=True,
        related_name='teacher_name'
    )
    teacher_award = models.ForeignKey(
        'teacherapp.T_award',
        on_delete=models.CASCADE,
        verbose_name='外键连接奖项表，当奖项信息被删除时，此数据也被删除',
        null=True
    )

    class Meta:
        db_table = 'teacherapp_teacher_award_zj'



class mb_setting(models.Model):
    teacher = models.ForeignKey('teacherapp.Teacher',
                                on_delete=models.SET_NULL,
                                verbose_name='外键连接，教师信息被删除时，此信息也被删除',
                                null=True)
    mb_name1 = models.CharField(verbose_name='密保问题1', db_column='Mb_name1', max_length=200)
    mb_answer1 = models.CharField(verbose_name='密保答案1', db_column='Mb_answer1', max_length=200)

    mb_name2 = models.CharField(verbose_name='密保问题2', db_column='Mb_name2', max_length=200)
    mb_answer2 = models.CharField(verbose_name='密保答案2', db_column='Mb_answer2', max_length=200)

    mb_name3 = models.CharField(verbose_name='密保问题3', db_column='Mb_name3', max_length=200)
    mb_answer3 = models.CharField(verbose_name='密保答案3', db_column='Mb_answer3', max_length=200)


# 假条流水记录
class Leave_zong(models.Model):
    id = models.CharField(verbose_name='假条id流水', max_length=200, primary_key=True)

    student_id = models.CharField(max_length=200, null=False)
    student_rname = models.CharField(null=False, max_length=200)
    student_class = models.CharField(null=False, max_length=200, verbose_name='学生班级')

    #     请假的内容详情

    start_time = models.DateTimeField(verbose_name='请假开始时间', max_length=200, null=False)
    stop_time = models.DateTimeField(verbose_name='请假结束时间', max_length=200, null=False)
    leave_cause = models.CharField(verbose_name='请假原因', max_length=200, null=False)

    approver_person = models.ForeignKey('teacherapp.Teacher', max_length=200, on_delete=models.CASCADE)

    approver_opinion = models.CharField(verbose_name='审批意见', null=True, max_length=200)
    APPROVER_OUT = [
        (1, '通过'),
        (2, '不通过'),
        (3, '审核中')
    ]
    approver_out = models.IntegerField(verbose_name='审批结果', null=False, choices=APPROVER_OUT)
    approver_time = models.DateTimeField(verbose_name='审批时间', null=True, blank=True, max_length=200)




# # 包含课程，学生，对应课程平时成绩和考试成绩，总成绩的中间表
class set_Grades_course(models.Model):
    course = models.ForeignKey('rootone.Course', on_delete=models.CASCADE, null=True)
    student = models.ForeignKey('studentapp.SStudents', on_delete=models.CASCADE, null=True)
    ps_gr = models.FloatField(verbose_name='平时成绩', null=True, validators=[MinValueValidator(0), MaxValueValidator(100)])
    ks_gr = models.FloatField(verbose_name='考试成绩', null=True, validators=[MinValueValidator(0), MaxValueValidator(100)])
    z_gr = models.FloatField(verbose_name='总成绩', null=True, validators=[MinValueValidator(0), MaxValueValidator(100)])
    cclass = models.ForeignKey('rootone.Class', on_delete=models.CASCADE, null=True)
    year = models.CharField(verbose_name='学年', null=True, max_length=6)
    QUARTER = [
        (1, '春季'),
        (2, '秋季')
    ]
    qua = models.IntegerField(verbose_name='季度选择', choices=QUARTER, null=True)